from apps.merchant.sku.dao import SkuDao
from apps.merchant.tag.dao import TagDao
from apps.merchant.item.dao import ItemDao
from apps.system.lessee.dao import LesseeDao
import datetime
import json

def setSeckillItem(params={}):
    ids = params.get('ids')
    lessee = LesseeDao().getLessee()
    dayDiscountItemParams = json.loads(lessee.dayDiscountItemParams)
    # print('dayDiscountItemParams', dayDiscountItemParams.get('2025-04-17'))
    if not ids or type(ids) != list:
        # disDict = {
        #     '2025-04-18': [1118,1098,1074,981],
        #     '2025-04-19': [1096,1131,982,946],
        # }
        # print('当天时间', datetime.datetime.now().strftime('%Y-%m-%d'))
        today = datetime.datetime.now().strftime('%Y-%m-%d')
        ids = dayDiscountItemParams.get(today)
        if not ids or type(ids) != list:
            ids = []
            return
    tag = TagDao().findAll().filter(name='秒杀').first()
    items = ItemDao().findAll().filter(tags__name='秒杀')
    for item in items:
        for sku in item.item_sku.all():
            sku.sellingPrice = sku.marketPrice
            sku.save()
        
        item.tags.remove(tag.id)
    items2 = ItemDao().findAll().filter(id__in=ids)
    for item in items2:
        for sku in item.item_sku.all():
            sku.marketPrice = sku.sellingPrice
            if sku.discountPrice > 0:
                sku.sellingPrice = sku.discountPrice
            sku.save()
        item.tags.add(tag.id)